const dbutil = require('../../../libs/MyDB');
const jwt = require('jsonwebtoken');

module.exports = {

    // 验证登录信息
    checkLogin: async function (data) {
        if (data.username == '' || data.password == '') {
            return {
                code: 403,
                msg: "用户名或密码不能为空"
            }
        }
        let result = {}
        await dbutil.singleSelect('res_user', 'user_passwd', `user_name='${data.username}'`)
            .then(
                res => {
                    result = res
                }
            )

        if (!result.user_passwd) {
            return {
                code: 401,
                msg: "用户名不存在"
            }
        } else {
            if (result.user_passwd != data.password) {
                return {
                    code: 402,
                    msg: "密码错误"
                }
            } else {
                const token = jwt.sign(
                    {
                        user_name:data.username,
                        user_passwd:data.password,
                    },
                    'secretkey',
                    { expiresIn: '2h' }
                );
                
                return {
                    code: 200,
                    msg: "登录成功",
                    url: "/backstage/index",
                    token:token
                }
            }
        }
    },
    // 获取各个表的数据条数
    getDataCount: async function () {
        let userCount = {};
        await dbutil.singleSelect('res_user', 'count(*) as count','1=1')
            .then(
                res => {
                    userCount = res.count;
                }
            )
        let foodCount = {};
        await dbutil.singleSelect('res_food', 'count(*) as count','1=1')
            .then(
                res => {
                    foodCount = res.count;
                }
            )
        let menuCount = {};
        await dbutil.singleSelect('res_menu', 'count(*) as count','1=1')
            .then(
                res => {
                    menuCount = res.count;
                }
            )
        let orderCount = {};
        await dbutil.singleSelect('res_order', 'count(*) as count','1=1')
            .then(
                res => {
                    orderCount = res.count;
                }
            )
        let tableCount = {};
        await dbutil.singleSelect('res_table', 'count(*) as count','1=1')
            .then(
                res => {
                    tableCount = res.count;
                }
            )
        let rowCount = {};
        await dbutil.singleSelect('res_row', 'count(*) as count','1=1')
            .then(
                res => {
                    rowCount = res.count;
                }
            )
          
        return {
            rowCount : rowCount,
            tableCount : tableCount,
            orderCount : orderCount,
            menuCount : menuCount,
            foodCount : foodCount,
            userCount : userCount
        };
    }
}